package bean.managed;

import entity.RawMaterialEntity;
import bean.managed.util.JsfUtil;
import bean.managed.util.PaginationHelper;
import bean.session.RawMaterialEntityFacade;

import entity.BatchEntity;
import entity.LocationQuantityEntity;
import entity.RawMaterialsLocationEntity;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import javax.faces.bean.ManagedProperty;
import javax.faces.component.UIViewRoot;
import javax.faces.validator.ValidatorException;

@ManagedBean(name = "rawMaterialEntityController")
@SessionScoped
public class RawMaterialEntityController implements Serializable {

    private RawMaterialEntity current;
    private DataModel items = null;
    private DataModel expiringBatchesInLocation = null;
    private BatchEntity batchToRemove = null;
    
    ArrayList<RawMaterialEntity> rawMaterialList;
    @EJB
    private bean.session.RawMaterialEntityFacade ejbFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;
    private int totalRMCapacity;
    private int totalRMUsage;
    @ManagedProperty(value = "#{rawMaterialsLocationEntityController}")
    RawMaterialsLocationEntityController rawMaterialsLocationEntityController;
    private RawMaterialsLocationEntity viewLocation;
    private int RMInViewLocation;
    List<LocationQuantityEntity> locationQuantities = new ArrayList<LocationQuantityEntity>();
    @ManagedProperty(value = "#{locationQuantityEntityController}")
    LocationQuantityEntityController locationQuantityEntityController;
    @ManagedProperty(value = "#{inventoryEntityController}")
    InventoryEntityController inventoryEntityController;
    @ManagedProperty(value = "#{batchEntityController}")
    BatchEntityController batchEntityController;
    @ManagedProperty(value = "#{disposalEntityController}")
    DisposalEntityController disposalEntityController;
    private RawMaterialsLocationEntity expiringRMInLocation;
    
    private String currentRMString;
    private int oats;
    private int wheatGerm;
    private int cinnamon;
    private int nutmeg;
    private int cloves;
    private int honey;
    private int canolaOil;
    private int vitMineralPowder;
    private int carobChips;
    private int proteinPowder;
    private int cocoaPowder;
    private int hazelnuts;
    private int cashewNuts;
    private int walnuts;
    private int almonds;
    private int macadamias;
    private int redcurrants;
    private int raisins;
    private int dates;
    private int apricots;
    private int kiwis;
    private int apples;
    private int peaches;
    private String rmLocationView;
    private int totalOats = 0;
    private int totalWheatGerm;
    private int totalCinnamon;
    private int totalNutmeg;
    private int totalCloves;
    private int totalHoney;
    private int totalCanolaOil;
    private int totalVitMineralPowder;
    private int totalCarobChips;
    private int totalProteinPowder;
    private int totalCocoaPowder;
    private int totalHazelnuts;
    private int totalCashewNuts;
    private int totalWalnuts;
    private int totalAlmonds;
    private int totalMacadamias;
    private int totalRedcurrants;
    private int totalRaisins;
    private int totalDates;
    private int totalApricots;
    private int totalKiwis;
    private int totalApples;
    private int totalPeaches;
    private int amtToRemove;

    public RawMaterialEntityController() {
    }
    
    public String prepareRawMaterialByLocation(){
        this.getRawMaterialsLocationEntityController().recreateModel();
        recreateModel();
        return "/entities/rawMaterialEntity/RawMaterialsByLocation?faces-redirect=true";
    }
    
    public ArrayList<RawMaterialEntity> getRawMaterialList(){
        rawMaterialList = new ArrayList<RawMaterialEntity>(getFacade().findAll());
        return rawMaterialList;
    }
    
    public void setRawMaterialList(ArrayList<RawMaterialEntity>list){
        rawMaterialList = list;
    }
    
     public String resetCreate() {
        recreateModel();
        return "Create?faces-redirect=true";
    }
     
     public String viewThisGood(RawMaterialEntity good){
        current = good;
        
        return "/entities/rawMaterialEntity/View?faces-redirect=true";
        
    }
     
      public String prepareEdit(RawMaterialEntity good) {
        current = good;
       
        return "Edit";
    }
      
      public String destroy(RawMaterialEntity good) {
        current = good;
        performDestroy();
        recreateModel();
        return "List";
        
    }
      
    public void validateMaterialName(FacesContext context, UIComponent validate, Object value) {

        String materialName = (String) value;
        if (ejbFacade.checkUserNameExist(materialName ,ejbFacade.findAll())) {
            FacesMessage message = new FacesMessage();
            message.setDetail("RawMaterial Name exists");
            message.setSummary("RawMaterial Name exists");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }
    }

    public String getCurrentRMString() {
        return currentRMString;
    }

    public void setCurrentRMString(String currentRMString) {
        current = this.getFacade().findRawMaterial(currentRMString);
        this.currentRMString = currentRMString;
    }

    public int getAmtToRemove() {
        return amtToRemove;
    }

    public void setAmtToRemove(int amtToRemove) {
        this.amtToRemove = amtToRemove;
    }

    public void setExpiringRMInLocation(RawMaterialsLocationEntity viewLocation) {
        this.viewLocation = viewLocation;

        Date currentDate = new Date();
        SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy");
        sdfDate.format(currentDate);
        Calendar c = Calendar.getInstance();
        c.setTime(currentDate);
        c.add(Calendar.DATE, 130);  // number of days to add

        List<BatchEntity> expiringBatches = new ArrayList<BatchEntity>();
        locationQuantities = this.getFacade().findAllRMInLocation(viewLocation.getId());
        System.out.println("LocationQuantities Size:" + locationQuantities.size());
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                if (locationQuantity.getInventory().getBatch().getExpiryDate().before(c.getTime()) && !expiringBatches.contains(locationQuantity.getInventory().getBatch())) {
                    if (locationQuantity.getRawMaterialsLocation().equals(viewLocation) && !locationQuantity.getInventory().getBatch().isArchived()) {
                        expiringBatches.add(locationQuantity.getInventory().getBatch());
                    }
                }
            }
        }
        this.expiringBatchesInLocation = new ListDataModel(expiringBatches);
        System.out.println(expiringBatchesInLocation.getRowCount());
    }

    public BatchEntity getBatchToRemove() {
        return batchToRemove;
    }

    public void setBatchToRemove(BatchEntity batchToRemove) {
        System.out.println("insetbatchtoremove");
        this.batchToRemove = batchToRemove;

        List<LocationQuantityEntity> locationToQuantityList = new ArrayList<LocationQuantityEntity>(batchToRemove.getInventory().getLocationToQuantity());
        for (int i = 0; i < locationToQuantityList.size(); i++) {
            LocationQuantityEntity locationQuantityEntity = locationToQuantityList.get(i);
            System.out.println(locationQuantityEntity.getId());
            System.out.println(viewLocation.getId());
            if (locationQuantityEntity.getRawMaterialsLocation().equals(viewLocation)) {
                locationQuantityEntity.setArchived(true);

                locationQuantityEntity.getInventory().setCurrentQuantity(locationQuantityEntity.getInventory().getCurrentQuantity() - locationQuantityEntity.getQuantity());
                viewLocation.setUsageKg(viewLocation.getUsageKg() - locationQuantityEntity.getQuantity());
                batchToRemove.getDisposal().setQuantity(batchToRemove.getDisposal().getQuantity() + locationQuantityEntity.getQuantity());

                getRawMaterialsLocationEntityController().getFacade().edit(viewLocation);
                getBatchEntityController().getEjbFacade().edit(locationQuantityEntity.getInventory().getBatch());
                getInventoryEntityController().getEjbFacade().edit(locationQuantityEntity.getInventory());
                getLocationQuantityEntityController().getEjbFacade().edit(locationQuantityEntity);
                getDisposalEntityController().getEjbFacade().edit(batchToRemove.getDisposal());
            }
        }
        FacesContext context = FacesContext.getCurrentInstance();
        String viewId = context.getViewRoot().getViewId();
        ViewHandler handler = context.getApplication().getViewHandler();
        UIViewRoot root = handler.createView(context, viewId);
        root.setViewId(viewId);
        context.setViewRoot(root);
    }

    public DisposalEntityController getDisposalEntityController() {
        return disposalEntityController;
    }

    public void setDisposalEntityController(DisposalEntityController disposalEntityController) {
        this.disposalEntityController = disposalEntityController;
    }

    public DataModel getExpiringBatchesInLocation() {
        return expiringBatchesInLocation;
    }

    public void setExpiringBatchesInLocation(DataModel expiringBatchesInLocation) {
        this.expiringBatchesInLocation = expiringBatchesInLocation;
    }

    public BatchEntityController getBatchEntityController() {
        return batchEntityController;
    }

    public void setBatchEntityController(BatchEntityController batchEntityController) {
        this.batchEntityController = batchEntityController;
    }

    public InventoryEntityController getInventoryEntityController() {
        return inventoryEntityController;
    }

    public void setInventoryEntityController(InventoryEntityController inventoryEntityController) {
        this.inventoryEntityController = inventoryEntityController;
    }

    public LocationQuantityEntityController getLocationQuantityEntityController() {
        return locationQuantityEntityController;
    }

    public void setLocationQuantityEntityController(LocationQuantityEntityController locationQuantityEntityController) {
        this.locationQuantityEntityController = locationQuantityEntityController;
    }

    public void removeRMFromInventory() {
        try {

            String productToPick = current.getProductName();
            System.out.println(productToPick + "--Product To Pick");
            LocationQuantityEntity locationToPick = null;
System.out.println(amtToRemove+"AMOUNT TO REMOVE");
            while (amtToRemove > 0) {
                locationToPick = firstToExpireLocation(productToPick);
                if (amtToRemove <= locationToPick.getQuantity()) {
                    locationToPick.setQuantity(locationToPick.getQuantity() - amtToRemove);
                    locationToPick.getInventory().setCurrentQuantity(locationToPick.getInventory().getCurrentQuantity() - amtToRemove);

                    RawMaterialsLocationEntity RMLocationEntity = (RawMaterialsLocationEntity) locationToPick.getLocation();
                    RMLocationEntity.setUsageKg(RMLocationEntity.getUsageKg() - amtToRemove);

                    this.getLocationQuantityEntityController().getEjbFacade().edit(locationToPick);
                    this.getRawMaterialsLocationEntityController().getEjbFacade().edit(RMLocationEntity);
                    this.getInventoryEntityController().getEjbFacade().edit(locationToPick.getInventory());
                    setAmtToRemove(0);
                } else if (amtToRemove > locationToPick.getQuantity()) {
                    locationToPick.getInventory().setCurrentQuantity(locationToPick.getInventory().getCurrentQuantity() - locationToPick.getQuantity());
                    locationToPick.setQuantity(0);

                    RawMaterialsLocationEntity RMLocationEntity = (RawMaterialsLocationEntity) locationToPick.getLocation();
                    RMLocationEntity.setUsageKg(RMLocationEntity.getUsageKg() - locationToPick.getQuantity());

                    this.getLocationQuantityEntityController().getEjbFacade().edit(locationToPick);
                    this.getRawMaterialsLocationEntityController().getEjbFacade().edit(RMLocationEntity);
                     this.getInventoryEntityController().getEjbFacade().edit(locationToPick.getInventory());
                    amtToRemove = amtToRemove - locationToPick.getQuantity();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private LocationQuantityEntity firstToExpireLocation(String productName) {
        System.out.println(productName);
        Calendar firstToExpire = null;
        LocationQuantityEntity locationQuantityToPick = null;

        List<LocationQuantityEntity> allLocationQuantities = new ArrayList<LocationQuantityEntity>(this.getFacade().findAllRM());
        for (LocationQuantityEntity locationQuantityEntity : allLocationQuantities) {
            if (productName.equals(locationQuantityEntity.getInventory().getBatch().getProduct().getProductName())) {

                if (firstToExpire == null || locationQuantityEntity.getInventory().getBatch().getExpiryDate().before(firstToExpire.getTime())) {
                    firstToExpire = Calendar.getInstance();
                    firstToExpire.setTime(locationQuantityEntity.getInventory().getBatch().getExpiryDate());
                    locationQuantityToPick = locationQuantityEntity;
                }
            }
        }
        System.out.println("here4" + locationQuantityToPick.getQuantity());
        return locationQuantityToPick;
    }

    public void resetQuantities() {
        this.setAlmonds(0);
        this.setApples(0);
        this.setApricots(0);
        this.setCanolaOil(0);
        this.setCarobChips(0);
        this.setCashewNuts(0);
        this.setCinnamon(0);
        this.setCloves(0);
        this.setCocoaPowder(0);
        this.setDates(0);
        this.setHazelnuts(0);
        this.setHoney(0);
        this.setKiwis(0);
        this.setMacadamias(0);
        this.setNutmeg(0);
        this.setOats(0);
        this.setPeaches(0);
        this.setProteinPowder(0);
        this.setRaisins(0);
        this.setRedcurrants(0);
        this.setVitMineralPowder(0);
        this.setWalnuts(0);
        this.setWheatGerm(0);

        this.setTotalAlmonds(0);
        this.setTotalApples(0);
        this.setTotalApricots(0);
        this.setTotalCanolaOil(0);
        this.setTotalCarobChips(0);
        this.setTotalCashewNuts(0);
        this.setTotalCinnamon(0);
        this.setTotalCloves(0);
        this.setTotalCocoaPowder(0);
        this.setTotalDates(0);
        this.setTotalHazelnuts(0);
        this.setTotalHoney(0);
        this.setTotalKiwis(0);
        this.setTotalMacadamias(0);
        this.setTotalNutmeg(0);
        this.setTotalOats(0);
        this.setTotalPeaches(0);
        this.setTotalProteinPowder(0);
        this.setTotalRaisins(0);
        this.setTotalRedcurrants(0);
        this.setTotalVitMineralPowder(0);
        this.setTotalWalnuts(0);
        this.setTotalWheatGerm(0);

    }

    public RawMaterialEntity getSelected() {
        if (current == null) {
            current = new RawMaterialEntity();
            selectedItemIndex = -1;
        }
        return current;
    }

    private RawMaterialEntityFacade getFacade() {
        return ejbFacade;
    }

    public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(10) {

                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
                }
            };
        }
        return pagination;
    }

    public String prepareList() {
        recreateModel();
        return "/entities/rawMaterialEntity/List?faces-redirect=true";
    }

    public String prepareView() {
        current = (RawMaterialEntity) getItems().getRowData();
        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        return "View";
    }

    public String prepareCreate() {
        current = new RawMaterialEntity();
        
        return "/entities/rawMaterialEntity/Create?faces-redirect=true";
    }

    public String create() {
        try {
            getFacade().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("RawMaterialEntityCreated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String prepareEdit() {
        current = (RawMaterialEntity) getItems().getRowData();
        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        return "Edit";
    }

    public String update() {
        try {
            getFacade().edit(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("RawMaterialEntityUpdated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy() {
        current = (RawMaterialEntity) getItems().getRowData();
        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        performDestroy();
        recreateModel();
        return "List";
    }
    
    
    

    public String destroyAndView() {
        performDestroy();
        recreateModel();
        updateCurrentItem();
        if (selectedItemIndex >= 0) {
            return "View";
        } else {
            // all items were removed - go back to list
            recreateModel();
            return "List";
        }
    }

    private void performDestroy() {
        try {
            getFacade().remove(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("RawMaterialEntityDeleted"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    private void updateCurrentItem() {
        int count = getFacade().count();
        if (selectedItemIndex >= count) {
            // selected index cannot be bigger than number of items:
            selectedItemIndex = count - 1;
            // go to previous page if last page disappeared:
            if (pagination.getPageFirstItem() >= count) {
                pagination.previousPage();
            }
        }
        if (selectedItemIndex >= 0) {
            current = getFacade().findRange(new int[]{selectedItemIndex, selectedItemIndex + 1}).get(0);
        }
    }

    public DataModel getItems() {
        if (items == null) {
            items = getPagination().createPageDataModel();
        }
        return items;
    }

    private void recreateModel() {
        items = null;
        current = null;
    }

    public String next() {
        getPagination().nextPage();
        recreateModel();
        return "List";
    }

    public String previous() {
        getPagination().previousPage();
        recreateModel();
        return "List";
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
    }

    @FacesConverter(forClass = RawMaterialEntity.class)
    public static class RawMaterialEntityControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            RawMaterialEntityController controller = (RawMaterialEntityController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "rawMaterialEntityController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Long getKey(String value) {
            java.lang.Long key;
            key = Long.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Long value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof RawMaterialEntity) {
                RawMaterialEntity o = (RawMaterialEntity) object;
                return getStringKey(o.getId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + RawMaterialEntityController.class.getName());
            }
        }
    }

    public int getRMInViewLocation() {
        return RMInViewLocation;
    }

    public void setRMInViewLocation(int RMInViewLocation) {
        this.RMInViewLocation = RMInViewLocation;
    }

    public int getAlmonds() {
        return almonds;
    }

    public void setAlmonds(int almonds) {
        this.almonds = almonds;
    }

    public int getApples() {
        return apples;
    }

    public void setApples(int apples) {
        this.apples = apples;
    }

    public int getApricots() {
        return apricots;
    }

    public void setApricots(int apricots) {
        this.apricots = apricots;
    }

    public int getCanolaOil() {
        return canolaOil;
    }

    public void setCanolaOil(int canolaOil) {
        this.canolaOil = canolaOil;
    }

    public int getCarobChips() {
        return carobChips;
    }

    public void setCarobChips(int carobChips) {
        this.carobChips = carobChips;
    }

    public int getCashewNuts() {
        return cashewNuts;
    }

    public void setCashewNuts(int cashewNuts) {
        this.cashewNuts = cashewNuts;
    }

    public int getCinnamon() {
        return cinnamon;
    }

    public void setCinnamon(int cinnamon) {
        this.cinnamon = cinnamon;
    }

    public int getCloves() {
        return cloves;
    }

    public void setCloves(int cloves) {
        this.cloves = cloves;
    }

    public int getCocoaPowder() {
        return cocoaPowder;
    }

    public void setCocoaPowder(int cocoaPowder) {
        this.cocoaPowder = cocoaPowder;
    }

    public int getDates() {
        return dates;
    }

    public void setDates(int dates) {
        this.dates = dates;
    }

    public int getHazelnuts() {
        return hazelnuts;
    }

    public void setHazelnuts(int hazelnuts) {
        this.hazelnuts = hazelnuts;
    }

    public int getHoney() {
        return honey;
    }

    public void setHoney(int honey) {
        this.honey = honey;
    }

    public int getKiwis() {
        return kiwis;
    }

    public void setKiwis(int kiwis) {
        this.kiwis = kiwis;
    }

    public List<LocationQuantityEntity> getLocationQuantities() {
        return locationQuantities;
    }

    public void setLocationQuantities(List<LocationQuantityEntity> locationQuantities) {
        this.locationQuantities = locationQuantities;
    }

    public int getMacadamias() {
        return macadamias;
    }

    public void setMacadamias(int macadamias) {
        this.macadamias = macadamias;
    }

    public int getNutmeg() {
        return nutmeg;
    }

    public void setNutmeg(int nutmeg) {
        this.nutmeg = nutmeg;
    }

    public int getOats() {
        return oats;
    }

    public void setOats(int oats) {
        this.oats = oats;
    }

    public int getPeaches() {
        return peaches;
    }

    public void setPeaches(int peaches) {
        this.peaches = peaches;
    }

    public int getProteinPowder() {
        return proteinPowder;
    }

    public void setProteinPowder(int proteinPowder) {
        this.proteinPowder = proteinPowder;
    }

    public int getRaisins() {
        return raisins;
    }

    public void setRaisins(int raisins) {
        this.raisins = raisins;
    }

    public RawMaterialsLocationEntityController getRawMaterialsLocationEntityController() {
        return rawMaterialsLocationEntityController;
    }

    public void setRawMaterialsLocationEntityController(RawMaterialsLocationEntityController rawMaterialsLocationEntityController) {
        this.rawMaterialsLocationEntityController = rawMaterialsLocationEntityController;
    }

    public int getRedcurrants() {
        return redcurrants;
    }

    public void setRedcurrants(int redcurrants) {
        this.redcurrants = redcurrants;
    }

    public String getRmLocationView() {
        return rmLocationView;
    }

    public void setRmLocationView(String rmLocationView) {
        this.rmLocationView = rmLocationView;
    }

    public int getSelectedItemIndex() {
        return selectedItemIndex;
    }

    public void setSelectedItemIndex(int selectedItemIndex) {
        this.selectedItemIndex = selectedItemIndex;
    }

    public int getTotalAlmonds() {
        setTotalAlmonds(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Almonds")) {
                    setTotalAlmonds(totalAlmonds + locationQuantity.getQuantity());
                }
            }
        }
        return totalAlmonds;
    }

    public void setTotalAlmonds(int totalAlmonds) {
        this.totalAlmonds = totalAlmonds;
    }

    public int getTotalApples() {
        setTotalApples(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Apples")) {
                    setTotalApples(totalApples + locationQuantity.getQuantity());
                }
            }
        }
        return totalApples;
    }

    public void setTotalApples(int totalApples) {
        this.totalApples = totalApples;
    }

    public int getTotalApricots() {
        setTotalApricots(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Apricots")) {
                    setTotalApricots(totalApricots + locationQuantity.getQuantity());
                }
            }
        }
        return totalApricots;
    }

    public void setTotalApricots(int totalApricots) {
        this.totalApricots = totalApricots;
    }

    public int getTotalCanolaOil() {
        setTotalCanolaOil(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Canola Oil")) {
                    setTotalCanolaOil(totalCanolaOil + locationQuantity.getQuantity());
                }
            }
        }
        return totalCanolaOil;
    }

    public void setTotalCanolaOil(int totalCanolaOil) {
        this.totalCanolaOil = totalCanolaOil;
    }

    public int getTotalCarobChips() {
        setTotalCarobChips(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Carob Chips")) {
                    setTotalCarobChips(totalCarobChips + locationQuantity.getQuantity());
                }
            }
        }
        return totalCarobChips;
    }

    public void setTotalCarobChips(int totalCarobChips) {
        this.totalCarobChips = totalCarobChips;
    }

    public int getTotalCashewNuts() {
        setTotalCashewNuts(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Cashew Nuts")) {
                    setTotalCashewNuts(totalCashewNuts + locationQuantity.getQuantity());
                }
            }
        }
        return totalCashewNuts;
    }

    public void setTotalCashewNuts(int totalCashewNuts) {
        this.totalCashewNuts = totalCashewNuts;
    }

    public int getTotalCinnamon() {
        setTotalCinnamon(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Cinnamon")) {
                    setTotalCinnamon(totalCinnamon + locationQuantity.getQuantity());
                }
            }
        }
        return totalCinnamon;
    }

    public void setTotalCinnamon(int totalCinnamon) {
        this.totalCinnamon = totalCinnamon;
    }

    public int getTotalCloves() {
        setTotalCloves(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Cloves")) {
                    setTotalCloves(totalCloves + locationQuantity.getQuantity());
                }
            }
        }
        return totalCloves;
    }

    public void setTotalCloves(int totalCloves) {
        this.totalCloves = totalCloves;
    }

    public int getTotalCocoaPowder() {
        setTotalCocoaPowder(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Cocoa Powder")) {
                    setTotalCocoaPowder(totalCocoaPowder + locationQuantity.getQuantity());
                }
            }
        }
        return totalCocoaPowder;
    }

    public void setTotalCocoaPowder(int totalCocoaPowder) {
        this.totalCocoaPowder = totalCocoaPowder;
    }

    public int getTotalDates() {
        setTotalDates(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Dates")) {
                    setTotalDates(totalDates + locationQuantity.getQuantity());
                }
            }
        }
        return totalDates;
    }

    public void setTotalDates(int totalDates) {
        this.totalDates = totalDates;
    }

    public int getTotalHazelnuts() {
        setTotalHazelnuts(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Hazelnuts")) {
                    setTotalHazelnuts(totalHazelnuts + locationQuantity.getQuantity());
                }
            }
        }
        return totalHazelnuts;
    }

    public void setTotalHazelnuts(int totalHazelnuts) {
        this.totalHazelnuts = totalHazelnuts;
    }

    public int getTotalHoney() {
        setTotalHoney(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Honey")) {
                    setTotalHoney(totalHoney + locationQuantity.getQuantity());
                }
            }
        }
        return totalHoney;
    }

    public void setTotalHoney(int totalHoney) {
        this.totalHoney = totalHoney;
    }

    public int getTotalKiwis() {
        setTotalKiwis(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Kiwis")) {
                    setTotalKiwis(totalKiwis + locationQuantity.getQuantity());
                }
            }
        }
        return totalKiwis;
    }

    public void setTotalKiwis(int totalKiwis) {
        this.totalKiwis = totalKiwis;
    }

    public int getTotalMacadamias() {
        setTotalMacadamias(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Macadamias")) {
                    setTotalMacadamias(totalMacadamias + locationQuantity.getQuantity());
                }
            }
        }
        return totalMacadamias;
    }

    public void setTotalMacadamias(int totalMacadamias) {
        this.totalMacadamias = totalMacadamias;
    }

    public int getTotalNutmeg() {
        setTotalNutmeg(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Nutmeg")) {
                    setTotalNutmeg(totalNutmeg + locationQuantity.getQuantity());
                }
            }
        }
        return totalNutmeg;
    }

    public void setTotalNutmeg(int totalNutmeg) {
        this.totalNutmeg = totalNutmeg;
    }

    public int getTotalOats() {
        setTotalOats(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Oats")) {
                    setTotalOats(totalOats + locationQuantity.getQuantity());
                }
            }
        }
        return totalOats;
    }

    public String prepareRMByLocation() {
        resetQuantities();
        totalRM();
        return "/entities/rawMaterialEntity/RawMaterialsByLocation?faces-redirect=true";
    }

    public void setTotalOats(int totalOats) {
        this.totalOats = totalOats;
    }

    public int getTotalPeaches() {
        setTotalPeaches(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Peaches")) {
                    setTotalPeaches(totalPeaches + locationQuantity.getQuantity());
                }
            }
        }
        return totalPeaches;
    }

    public void setTotalPeaches(int totalPeaches) {
        this.totalPeaches = totalPeaches;
    }

    public int getTotalProteinPowder() {
        setTotalProteinPowder(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Protein Powder")) {
                    setTotalProteinPowder(totalProteinPowder + locationQuantity.getQuantity());
                }
            }
        }
        return totalProteinPowder;
    }

    public void setTotalProteinPowder(int totalProteinPowder) {
        this.totalProteinPowder = totalProteinPowder;
    }

    public int getTotalRMCapacity() {
        totalRMCapacity = 0;
        List<RawMaterialsLocationEntity> rawMaterialsLocations = new ArrayList<RawMaterialsLocationEntity>(getRawMaterialsLocationEntityController().getFacade().findAllIncludeArchived());

        for (int i = 0; i < rawMaterialsLocations.size(); i++) {
            totalRMCapacity = totalRMCapacity + rawMaterialsLocations.get(i).getCapacityKg();
        }
        return totalRMCapacity;
    }

    public void setTotalRMCapacity(int totalRMCapacity) {
        this.totalRMCapacity = totalRMCapacity;
    }

    public int getTotalRMUsage() {
        totalRMUsage = 0;
        List<RawMaterialsLocationEntity> rawMaterialsLocations = new ArrayList<RawMaterialsLocationEntity>(getRawMaterialsLocationEntityController().getFacade().findAllIncludeArchived());

        for (int i = 0; i < rawMaterialsLocations.size(); i++) {
            totalRMUsage = totalRMUsage + rawMaterialsLocations.get(i).getUsageKg();
        }
        return totalRMUsage;
    }

    public void setTotalRMUsage(int totalRMUsage) {
        this.totalRMUsage = totalRMUsage;
    }

    public int getTotalRaisins() {
        setTotalRaisins(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Raisins")) {
                    setTotalRaisins(totalRaisins + locationQuantity.getQuantity());
                }
            }
        }
        return totalRaisins;
    }

    public void setTotalRaisins(int totalRaisins) {
        this.totalRaisins = totalRaisins;
    }

    public int getTotalRedcurrants() {
        setTotalRedcurrants(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Recurrants")) {
                    setTotalRedcurrants(totalRedcurrants + locationQuantity.getQuantity());
                }
            }
        }
        return totalRedcurrants;
    }

    public void setTotalRedcurrants(int totalRedcurrants) {
        this.totalRedcurrants = totalRedcurrants;
    }

    public int getTotalVitMineralPowder() {
        setTotalVitMineralPowder(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Walnuts")) {
                    setTotalVitMineralPowder(totalVitMineralPowder + locationQuantity.getQuantity());
                }
            }
        }
        return totalVitMineralPowder;
    }

    public void setTotalVitMineralPowder(int totalVitMineralPowder) {
        this.totalVitMineralPowder = totalVitMineralPowder;
    }

    public int getTotalWalnuts() {
        setTotalWalnuts(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Walnuts")) {
                    setTotalWalnuts(totalWalnuts + locationQuantity.getQuantity());
                }
            }
        }
        return totalWalnuts;
    }

    public void setTotalWalnuts(int totalWalnuts) {
        this.totalWalnuts = totalWalnuts;
    }

    public int getTotalWheatGerm() {
        setTotalWheatGerm(0);
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Wheat Germ")) {
                    setTotalWheatGerm(totalWheatGerm + locationQuantity.getQuantity());
                }
            }
        }
        return totalWheatGerm;
    }

    public void setTotalWheatGerm(int totalWheatGerm) {
        this.totalWheatGerm = totalWheatGerm;
    }

    public RawMaterialsLocationEntity getViewLocation() {
        if (viewLocation == null) {
            viewLocation = new RawMaterialsLocationEntity();
        }
        return viewLocation;
    }

    public void setViewLocation(RawMaterialsLocationEntity viewLocation) {
        System.out.print("inside here");
        System.out.print(viewLocation.getId());
        resetQuantities();
        locationQuantities = this.getFacade().findAllRMInLocation(viewLocation.getId());
        System.out.println("locationquantitysize"+locationQuantities.size());
        
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            System.out.println("inventoryid"+locationQuantity.getInventory().getId());
            String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();

            if (productName.equals("Oats")) {
                // System.out.println("in oats");
                setOats(getOats() + locationQuantity.getQuantity());
            } else if (productName.equals("Wheat Germ")) {
                setWheatGerm(getWheatGerm() + locationQuantity.getQuantity());
            } else if (productName.equals("Cinnamon")) {
                setCinnamon(getCinnamon() + locationQuantity.getQuantity());
            } else if (productName.equals("Nutmeg")) {
                setNutmeg(getNutmeg() + locationQuantity.getQuantity());
            } else if (productName.equals("Cloves")) {
                setCloves(getCloves() + locationQuantity.getQuantity());
            } else if (productName.equals("Honey")) {
                setHoney(getHoney() + locationQuantity.getQuantity());
            } else if (productName.equals("Canola Oil")) {
                setCanolaOil(getCanolaOil() + locationQuantity.getQuantity());
            } else if (productName.equals("Vit Mineral Powder")) {
                setVitMineralPowder(getVitMineralPowder() + locationQuantity.getQuantity());
            } else if (productName.equals("Carob Chips")) {
                setCarobChips(getCarobChips() + locationQuantity.getQuantity());
            } else if (productName.equals("Protein Powder")) {
                setProteinPowder(getProteinPowder() + locationQuantity.getQuantity());
            } else if (productName.equals("Cocoa Powder")) {
                setCocoaPowder(getCocoaPowder() + locationQuantity.getQuantity());
            } else if (productName.equals("Hazel Nuts")) {
                setHazelnuts(getHazelnuts() + locationQuantity.getQuantity());
            } else if (productName.equals("Cashew Nuts")) {
                setCashewNuts(getCashewNuts() + locationQuantity.getQuantity());
            } else if (productName.equals("Walnuts")) {
                setWalnuts(getWalnuts() + locationQuantity.getQuantity());
            } else if (productName.equals("Almonds")) {
                setAlmonds(getAlmonds() + locationQuantity.getQuantity());
            } else if (productName.equals("Macadamias")) {
                setMacadamias(getMacadamias() + locationQuantity.getQuantity());
            } else if (productName.equals("Redcurrants")) {
                setRedcurrants(getRedcurrants() + locationQuantity.getQuantity());
            } else if (productName.equals("Raisins")) {
                setRaisins(getRaisins() + locationQuantity.getQuantity());
            } else if (productName.equals("Dates")) {
                setDates(getDates() + locationQuantity.getQuantity());
            } else if (productName.equals("Apricots")) {
                setApricots(getApricots() + locationQuantity.getQuantity());
            } else if (productName.equals("Kiwis")) {
                setKiwis(getKiwis() + locationQuantity.getQuantity());
            } else if (productName.equals("Apples")) {
                setApples(getApples() + locationQuantity.getQuantity());
            } else if (productName.equals("Peaches")) {
                setPeaches(getPeaches() + locationQuantity.getQuantity());
            }
        }
    }

    public int getVitMineralPowder() {
        return vitMineralPowder;
    }

    public void setVitMineralPowder(int vitMineralPowder) {
        this.vitMineralPowder = vitMineralPowder;
    }

    public int getWalnuts() {
        return walnuts;
    }

    public void setWalnuts(int walnuts) {
        this.walnuts = walnuts;
    }

    public int getWheatGerm() {
        return wheatGerm;
    }

    public void setWheatGerm(int wheatGerm) {
        this.wheatGerm = wheatGerm;
    }

    public void totalRM() {
        resetQuantities();
        locationQuantities = this.getFacade().findAllRM();
        for (LocationQuantityEntity locationQuantity : locationQuantities) {
            if (locationQuantity.getType().equals("RM")) {
                String productName = locationQuantity.getInventory().getBatch().getProduct().getProductName();
                if (productName.equals("Oats")) {

                    setTotalOats(totalOats + locationQuantity.getQuantity());
                    System.out.println(getOats());
                } else if (productName.equals("Wheat Germ")) {
                    setTotalWheatGerm(totalWheatGerm + locationQuantity.getQuantity());
                } else if (productName.equals("Cinnamon")) {
                    setTotalCinnamon(totalCinnamon + locationQuantity.getQuantity());
                } else if (productName.equals("Nutmeg")) {
                    setTotalNutmeg(totalNutmeg + locationQuantity.getQuantity());
                } else if (productName.equals("Cloves")) {
                    setTotalCloves(totalCloves + locationQuantity.getQuantity());
                } else if (productName.equals("Honey")) {
                    setTotalHoney(totalHoney + locationQuantity.getQuantity());
                } else if (productName.equals("Canola Oil")) {
                    setTotalCanolaOil(totalCanolaOil + locationQuantity.getQuantity());
                } else if (productName.equals("Vit Mineral Powder")) {
                    setTotalVitMineralPowder(totalVitMineralPowder + locationQuantity.getQuantity());
                } else if (productName.equals("Carob Chips")) {
                    setTotalCarobChips(totalCarobChips + locationQuantity.getQuantity());
                } else if (productName.equals("Protein Powder")) {
                    setTotalProteinPowder(totalProteinPowder + locationQuantity.getQuantity());
                } else if (productName.equals("Cocoa Powder")) {
                    setTotalCocoaPowder(totalCocoaPowder + locationQuantity.getQuantity());
                } else if (productName.equals("Hazel Nuts")) {
                    setTotalHazelnuts(totalHazelnuts + locationQuantity.getQuantity());
                } else if (productName.equals("Cashew Nuts")) {
                    setTotalCashewNuts(totalCashewNuts + locationQuantity.getQuantity());
                } else if (productName.equals("Walnuts")) {
                    setTotalWalnuts(totalWalnuts + locationQuantity.getQuantity());
                } else if (productName.equals("Almonds")) {
                    setTotalAlmonds(totalAlmonds + locationQuantity.getQuantity());
                } else if (productName.equals("Macadamias")) {
                    setTotalMacadamias(totalMacadamias + locationQuantity.getQuantity());
                } else if (productName.equals("Redcurrants")) {
                    setTotalRedcurrants(totalRedcurrants + locationQuantity.getQuantity());
                } else if (productName.equals("Raisins")) {
                    setTotalRaisins(totalRaisins + locationQuantity.getQuantity());
                } else if (productName.equals("Dates")) {
                    setTotalDates(totalDates + locationQuantity.getQuantity());
                } else if (productName.equals("Apricots")) {
                    setTotalApricots(totalApricots + locationQuantity.getQuantity());
                } else if (productName.equals("Kiwis")) {
                    setTotalKiwis(totalKiwis + locationQuantity.getQuantity());
                } else if (productName.equals("Apples")) {
                    setTotalApples(totalApples + locationQuantity.getQuantity());
                } else if (productName.equals("Peaches")) {
                    setTotalPeaches(totalPeaches + locationQuantity.getQuantity());
                }
            }
        }
    }
}
